package com.cx.purchase.subclass.service;


import com.cx.purchase.config.ResultVO;
import com.cx.purchase.config.ResultVOBuilder;
import com.cx.purchase.subclass.mapper.SubclassMapper;
import com.cx.purchase.subclass.model.*;
import com.cx.purchase.util.AllrecordsVo;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class PurchaseServiceImpl implements PurchaseService{
    @Autowired
    private SubclassMapper subClassMapper;
    @Override
    public ResultVO queryPage(SubClassSelectVo subClassSelectVo) {


        List<Subclass> subClassVoList = subClassMapper.queryData(subClassSelectVo);

        for (Subclass subclass:
        subClassVoList) {
            if (subclass.getStatus()==true) {
                subclass.setStatusName("启用");
            }else{
                subclass.setStatusName("禁用");
            }

        }
        int count =  subClassMapper.queryCount(subClassSelectVo);

        AllrecordsVo allrecordsVo = new AllrecordsVo();
        allrecordsVo.setPageIndex(subClassSelectVo.getPageIndex());
        allrecordsVo.setPageSize(subClassSelectVo.getPageSize());
        allrecordsVo.setTotalNumber(count);
        allrecordsVo.setDataList(subClassVoList);
        allrecordsVo.resettotalNum(count);
        return ResultVOBuilder.success(allrecordsVo);

    }

    @Override
    @Transactional
    public ResultVO add(@Valid SubClassAddVo subClassAddVo) {
        //验证传过来的编码或者名称是否在数据库中已经存在
        //如果存在提示前端错误消息
        Subclass subclass = subClassMapper.queryByCode(subClassAddVo.getCode());
        if(subclass!=null){
            return ResultVOBuilder.error("已存在改编码,请检查数据");
        }
        Subclass subclassName = subClassMapper.queryByName(subClassAddVo.getName());
        if(subclassName!=null) {
            return ResultVOBuilder.error("已存在该名称,请检查数据");
        }

        //入库
        //由于没用创建人 和修改人  就给他一个
        String name = "张三";
        Subclass subclass1 = new Subclass();
        //由于目前对象没有添加的功能所以将添加的功能付给 目前的这个对象
        BeanUtils.copyProperties(subClassAddVo,subclass1);
        subclass1.setCreateName(name);
        subclass1.setCreateTime(new Date());
        subclass1.setUpdateName(name);
        subclass1.setUpdateTime(new Date());

        subClassMapper.add(subclass1);
        return ResultVOBuilder.success(subclass1);
    }

    public ResultVO update(SubClassUpdateVo subClassUpdateVo) {


            //验证传过来的编码或者名称是否在数据库中已经存在
            //如果存在提示前端错误消息
            Subclass subclassId = subClassMapper.queryById(subClassUpdateVo.getId());
            if(subclassId==null){
                return ResultVOBuilder.error("数据不存在,请检查数据");
            }
            Subclass subclassName = subClassMapper.queryByName(subClassUpdateVo.getName());
            if(subclassName!=null &&  subclassId.getId() != subClassUpdateVo.getId()){
                return ResultVOBuilder.error("已存在该名称,请检查数据");
            }
            //入库
            //由于没用创建人 和修改人  就给他一个
            String name = "张三";
            Subclass subclass1 = new Subclass();
            //由于目前对象没有添加的功能所以将添加的功能付给 目前的这个对象
            BeanUtils.copyProperties(subClassUpdateVo,subclass1);
            subclass1.setUpdateName(name);
            subclass1.setUpdateTime(new Date());



            int i = subClassMapper.update(subclass1);

            if(i==0){
                return ResultVOBuilder.error("其他用户已经修改该数据,请刷新后重试");
            }
            return ResultVOBuilder.success(subclass1);



    }

    public ResultVO queryById(Integer id) {
        //查看数据库中是否又该id
        Subclass subclassId = subClassMapper.queryById(id);
        if(subclassId==null){
            return ResultVOBuilder.error("数据不存在,请检查数据");
        }
        return ResultVOBuilder.success(subclassId);
    }

    public ResultVO updateStatus(SubClassUpdateStatusVo subClassUpdateStatusVo) {
        //验证传过来的编码或者名称是否在数据库中已经存在
        //如果存在提示前端错误消息
        Subclass subclassId = subClassMapper.queryById(subClassUpdateStatusVo.getId());
        if(subclassId==null){
            return ResultVOBuilder.error("数据不存在,请检查数据");
        }

        //入库
        //由于没用创建人 和修改人  就给他一个
        String name = "张三";
        Subclass subclass1 = new Subclass();
        //由于目前对象没有添加的功能所以将添加的功能付给 目前的这个对象
        BeanUtils.copyProperties(subClassUpdateStatusVo,subclass1);
        subclass1.setUpdateName(name);
        subclass1.setUpdateTime(new Date());

        subClassMapper.updateStatus(subclass1);
        return ResultVOBuilder.success(subclass1);
    }


}
